{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This version explicitly checks for None and uses Optional from the typing module. It follows the fail-fast principle by validating inputs at the start."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.5\n"
     ]
    }
   ],
   "source": [
    "from typing import Optional, Iterable\n",
    "\n",
    "\n",
    "def calculate_average_v1(\n",
    "    numbers: Optional[Iterable[int | float]], precision: Optional[int] = None\n",
    ") -> float:\n",
    "    # Fail fast: Check for None and empty list at the start\n",
    "    if numbers is None:\n",
    "        raise ValueError(\"Numbers cannot be None.\")\n",
    "    if not numbers:\n",
    "        raise ValueError(\"Numbers cannot be an empty list.\")\n",
    "\n",
    "    average = sum(numbers) / len(numbers)\n",
    "\n",
    "    # Check precision if provided\n",
    "    if precision is not None:\n",
    "        if precision < 0:\n",
    "            raise ValueError(\"Precision cannot be negative.\")\n",
    "        return round(average, precision)\n",
    "\n",
    "    return average\n",
    "\n",
    "\n",
    "# Example usage\n",
    "print(calculate_average_v1([1, 2, 3, 4, 5], precision=2))  # Output: 3.0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This version avoids Optional by using default values directly and minimizing None checks."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.5\n"
     ]
    }
   ],
   "source": [
    "def calculate_average_v2(numbers: Iterable[int | float], precision: int = 2) -> float:\n",
    "    # Fail fast: Check for empty list early\n",
    "    if not numbers:\n",
    "        raise ValueError(\"Numbers cannot be an empty list.\")\n",
    "\n",
    "    average = sum(numbers) / len(numbers)\n",
    "    return round(average, precision)\n",
    "\n",
    "\n",
    "# Example usage\n",
    "print(calculate_average_v2([1, 2, 3, 4, 5]))  # Output: 3.0"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
